package com.tmc.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.bean.intair.EthShoppingReq;
import com.tmc.constant.FareConstant.PolicyStatus;
import com.tmc.entity.intair.PrivateFare;
import com.tmc.util.DateUtil;
import com.tmc.util.ObjectUtil;

@Repository
public class PrivateFareDaoImpl extends BaseDaoImpl<PrivateFare> {

	public List<PrivateFare> findPolicys(ContextInfo context, EthShoppingReq req) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from PrivateFare c where c.enabledFlag = 'Y'");
		// 匹配政策已上架
		sql.append(" and c.status= :status");
		params.put("status",  PolicyStatus.UP.getStatus());
		// 匹配飞行类型
		sql.append(" and (c.flightType is null or c.flightType like :flightType )");
		params.put("flightType", "%" + req.getFlightType() + "%");
		//匹配开票时间
		sql.append(" and (c.ticketingDateStart <= :currentTime");
		sql.append(" and c.ticketingDateEnd >= :currentTime)");
		params.put("currentTime", DateUtil.toLongDate(//
				DateUtil.toStrDate(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd"));
		//匹配航司
		if (ObjectUtil.hasText(req.getAirline())) {
			sql.append(" and c.airline = :airline");
			params.put("airline", req.getAirline());
		}
		return this.findList(sql.toString(), params);
	}

	public Pagination<PrivateFare> findPolicysByCon(ContextInfo context, PrivateFare cond, int pageIndex,
			int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from PrivateFare c where c.enabledFlag = 'Y' ");

		if (ObjectUtil.hasText(cond.getDeparture())) {
			sql.append(" and (c.departure is null or c.departure like :departure ) ");
			params.put("departure", "%" + cond.getDeparture() + "%");
		}
		if (ObjectUtil.hasText(cond.getArrival())) {
			sql.append(" and (c.arrival is null or c.arrival like :arrival )");
			params.put("arrival", "%" + cond.getArrival() + "%");
		}
		if (ObjectUtil.hasText(cond.getOutboundTravelDateStartStr())) {
			sql.append(" and (c.outboundTravelDateStart is null or c.outboundTravelDateStart <= :startDate )");
			params.put("startDate", DateUtil.toLongDate(cond.getOutboundTravelDateStartStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getOutboundTravelDateEndStr())) {
			sql.append(" and (c.outboundTravelDateEnd is null or c.outboundTravelDateEnd >= :endDate )");
			params.put("endDate", DateUtil.toLongDate(cond.getOutboundTravelDateEndStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getAirline())) {
			sql.append(" and c.airline = :airline");
			params.put("airline", cond.getAirline());
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
}
